package org.myorg.v4;

public class GA42 {
	static int best_p;
	static double best_v;
	static FPeople4 best_people;


	public static String ga(int geneLength,
			int peopleNum, double p_cross,
			double p_mut, long i_num,Fc4 fc) {

		String res = "";
		String value = "";
		String time = "";
		String it = "";
		double g_b = Double.NEGATIVE_INFINITY;

		long start_time = System.currentTimeMillis();
		FPeople4 my_p = new FPeople4(peopleNum,geneLength);
		FEvolution4 my_e = new FEvolution4(0.1, p_cross, p_mut);
		//fc.caculate(my_p.persons, 1, 4);
		fc.caculate(my_p.persons);
		for (int i=0; i < i_num; i++) {

			/*if current best value are letter than global best value,
			 * replace global best value with current value
			 */
/*			double c_b = my_p.getBest().fitness;
			if (c_b > g_b) {
				g_b = c_b;
				int is_r = fc.is_r(my_p.getBest());
				if (1 == is_r ||  0 == is_r ) {
					res = res + "\t" + (System.currentTimeMillis() - start_time) 
					+"\t" + i + "\t" + fc.org_value(my_p.getBest());
					if (0 == is_r ) {
						break;
					}
				}
			}*/
			
//			if (i%1000 == 0 || i == i_num - 1) {
/*				System.out.println("best gene " + (my_p.getBest().geneAt(0)*(fc.high-fc.low)+fc.low) 
						+ "\t" + (my_p.getBest().geneAt(1)*(fc.high-fc.low)+fc.low));
*/			
/*				System.out.println("best gene " + (my_p.getBest().geneAt(0)*100) 
						+ "\t" + (my_p.getBest().geneAt(1)*100));*/
			//System.out.println("best gene " + (my_p.getBest().geneAt(0)*(fc.high-fc.low)+fc.low));
				//System.out.print(i + "best gene ");
/*				for (int v = my_p.getBest().length - 1; v > my_p.getBest().length - 4; v--) {
					System.out.print(my_p.getBest().geneAt(v)*100 + " ");	
				}*/
/*				for (int v = 0; v < my_p.getBest().length; v++) {
					System.out.print(my_p.getBest().geneAt(v)*200-100 + " ");	
				}*/
				//for (int v = 0; v < my_p.getBest().length; v++) {
//				double tem = Integer.parseInt(my_p.getBest().gene, 2);
//				double scope = Math.pow(2, my_p.getBest().length) - 1;
//				double x = tem/scope;
//					System.out.print(x);	
				//}
			
//				System.out.print("\n");
			
				value = value + "\t" + -my_p.getBest().fitness;
			time = time + "\t" + (System.currentTimeMillis() - start_time);
			it = it + "\t" + i;
//			}
			if (i%100 == 0 || i == i_num - 1) {
				System.out.println(i + " value is " + my_p.getBest().fitness);
			}
			
			//if ( 1.0d - my_p.getBest().fitness < 1.0E-4) 
			//	break;
			
			
			my_e.select(my_p);
			my_e.crossOver(my_p);
			my_e.mutation(my_p);
			//fc.caculate(my_p.persons, 1, 4);
			fc.caculate(my_p.persons);
		}

		res = value + "\n" + time + "\n" + it + "\n";
		
		return res;
	}
	
	public static void main(String[] arg) {
		System.out.println("enter main");
		double[] data = {10E6, 10E5}; 
		//double[] data = {24, 18, 4, 4, 2, 1}; 
		int dim = 63;
		//Fc4 fc = new FFZ41(dim);
		Fc4 fc = new F43();
		//double[] data = { 0.9, 0.99, 0.999, 0.9999, 0.99999};
		//double[] data = {0.01, 0.1, 0.2, 0.4, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.95, 0.999};
		//double[] data = {1.0E-12, 5.0E-11, 4.0E-11, 4.0E-11 , 2.0E-11};
		//double[] data = {1.0E12, 5.0E11, 4.0E11, 4.0E11 , 1.0E10, 1.0E09, 1.0E08, 1.0E07};
		String tmp = ga(dim, 20, 0.1, 0.01, Long.MAX_VALUE, fc);
		System.out.println(tmp);
		
		System.out.println("leave main");
	}
}
